package com.xtayfjpk.thrift.test.server;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;

import com.xtayfjpk.thrift.test.service.HelloWorldService;
import com.xtayfjpk.thrift.test.service.HelloWorldService.AsyncClient.sayHello_call;

public class AsyncHelloWorldClient {
	public static void main(String[] args) throws Exception {
		TAsyncClientManager clientManager = new TAsyncClientManager();
		TNonblockingTransport transport = new TNonblockingSocket("127.0.0.1", 8090, 6000);
		
		
		//TProtocol protocol = new TBinaryProtocol(transport);
		TProtocolFactory protocol = new TCompactProtocol.Factory();
		HelloWorldService.AsyncClient client = new HelloWorldService.AsyncClient(protocol, clientManager, transport);
		
		final CountDownLatch countDownLatch = new CountDownLatch(1);
		client.sayHello("张三", new AsyncMethodCallback<sayHello_call>() {

			@Override
			public void onComplete(sayHello_call response) {
				try {
					System.out.println(response.getResult());
				} catch (TException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				countDownLatch.countDown();
			}

			@Override
			public void onError(Exception exception) {
				System.out.println(exception);
				countDownLatch.countDown();
			}
		});
		
		countDownLatch.await(10, TimeUnit.SECONDS);
	}
}
